package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Model.Player;
import Model.Tile;

/**
 * Contains functionality to draw the mountains, river, and town squares on the map.
 * Each panel contains one tile.
 * @author Matt Miller
 *
 */
public class TilePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private Tile tile;
	private Player owner;
	private String type;
	
	/**
	 * Constructs the tile Panel
	 * @param tile
	 * @throws IOException 
	 */
	public TilePanel(Tile tile) throws IOException {
		
		this.tile = tile;
		setPreferredSize(new Dimension(130, 160));
		owner = tile.getOwner();
		type = tile.getType();	
		setVisible(true);
		JLabel mulePic = new JLabel();
		try
		{	
			BufferedImage muleImage = ImageIO.read(new File("mule.png"));
			ImageIcon muleIcon = new ImageIcon(muleImage);
			
			mulePic.setIcon(muleIcon);
			if (tile.getMuleHere()) {
				this.add(mulePic);
			}
			
		}
		catch(IOException e) 
		{
			System.out.println("Image file not found");
		}
		
		
	}

	/**
	 * Contains functionality to draw map icons
	 * @param Graphics g
	 */
	public void paintComponent(Graphics g) {
		if(type.equals("R")) {
			g.setColor(Color.BLUE);
			g.fillRect(60, 0, 10, 160);
		} else if (type.equals("M1")) {
			g.setColor(Color.BLACK);
			g.drawArc(0, 80, 110, 60, 0, 180);
		} else if (type.equals("M2")) {
			g.setColor(Color.BLACK);
			g.drawArc(0, 60, 110, 60, 0, 180);
			g.drawArc(0, 100, 110, 60, 0, 180);
		} else if (type.equals("M3")) {
			g.setColor(Color.BLACK);
			g.drawArc(0, 40, 110, 40, 0, 180);
			g.drawArc(0, 80, 110, 40, 0, 180);
			g.drawArc(0, 120, 110, 40, 0, 180);
		} else if (type.equals("T")) {
			g.setColor(Color.ORANGE);
			g.fillRect(0, 0, 130, 160);
		}
		if(owner == null) {
			g.setColor(Color.WHITE);
		} else {
			if(owner.getColor().equals("RED")) {
				g.setColor(Color.RED);
			} else if(owner.getColor().equals("BLUE")) {
				g.setColor(Color.BLUE);
			} else if(owner.getColor().equals("GREEN")) {
				g.setColor(Color.GREEN);
			} else {
				g.setColor(Color.YELLOW);
			}
		}
		g.fillRect(0, 0, 130, 5);
		g.fillRect(0, 0, 5, 160);
		g.fillRect(0, 150, 130, 5);
		g.fillRect(125, 0, 5, 160);
		
	}
}
